//
//  SKUManager.m
//  B2C
//
//  Created by xiaowu on 2017/8/10.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "SKUManager.h"

@interface SKUManager ()

//@property (nonatomic, strong) NSMutableArray <GoodAttributeModel *> *attrArray;  // 所有属性
//@property (nonatomic, strong) NSMutableArray <GoodsAttributePriceModel *> *priceSkuArray; // 所有价格sku数组

@end

@implementation SKUManager

// 获取已选中的属性值
+ (NSMutableArray <GoodAttributeValueModel *> *)getSelectAttrWithAllAttributeArr:(NSMutableArray <GoodAttributeModel *> *)attrArray {
    NSMutableArray *arr = [NSMutableArray array];
    for (GoodAttributeModel *goodAttributeModel in attrArray) {
        for (GoodAttributeValueModel *valueModel in goodAttributeModel.twoattribute) {
            if (valueModel.isSelected == YES) {
                [arr addObject:valueModel];
            }
        }
    }
    return arr;
}

// 根据已经选择得属性值，得到余下还能选择的属性值
+ (NSMutableArray <NSString *> *)getFilterAttrs:(NSMutableArray <GoodAttributeValueModel *> *)select_Attrids
                                withPriceSkuArr:(NSMutableArray <GoodsAttributePriceModel *> *)priceSkuArr
                                andAttributeArr:(NSMutableArray <GoodAttributeModel *> *)attributeArr {
    NSMutableArray *arr = [NSMutableArray array];
    
    NSMutableArray *temp = [self getFilterProduct:select_Attrids withPriceSkuArr:priceSkuArr];
    
    
    for (GoodsAttributePriceModel *priceModel in temp) {
        for (TwoAtrributeModel *twoModel in priceModel.twoattribute) {
            if (![arr containsObject:twoModel.taid]) {
                [arr addObject:twoModel.taid];
            }
        }
    }
    
    return arr;
}

//获取所有包含指定节点的路线
+ (NSMutableArray <GoodsAttributePriceModel *> *)getFilterProduct:(NSMutableArray <GoodAttributeValueModel *> *)ids
                                                  withPriceSkuArr:(NSMutableArray <GoodsAttributePriceModel *> *)priceSkuArr {
    NSMutableArray *arr = [NSMutableArray array];
    for (NSInteger i = 0; i < priceSkuArr.count; i++) {
        GoodsAttributePriceModel *priceModel = [priceSkuArr safeObjectAtIndex:i];
        NSString *str = priceModel.taids;
        BOOL all_ids_in = YES;
        for (GoodAttributeValueModel *valueModel in ids) {
            if (![str containsString:valueModel.taid]) {
                all_ids_in = NO; // sku 不包含该属性
            }
        }
        if (all_ids_in) {
            [arr addObject:priceModel];
        }
    }
    return arr;
}


/**
 设置对象是否可选状态
 
 @param attrArray 原始属性数组
 @param valueArr 可选属性数组
 @return 返回属性是否可选的数组
 */
+ (NSMutableArray <GoodAttributeModel *> *)setSelectState:(NSMutableArray <GoodAttributeModel *> *)attrArray value:(NSMutableArray <NSString *> *)valueArr {
    for (GoodAttributeModel *attributeModel in attrArray) {
        for (GoodAttributeValueModel *valueModel in attributeModel.twoattribute) {
            if ([valueArr containsObject:valueModel.taid]) {
                valueModel.canSelect = YES;
            } else {
                valueModel.canSelect = NO;
            }
        }
    }
    return attrArray;
}



+ (BOOL)allAttributeSelect:(NSMutableArray <GoodAttributeModel *> *)attrArray {
    NSMutableArray *selectArr = [NSMutableArray array];
    for (GoodAttributeModel *model in attrArray) {
        NSNumber *isSelect = @(0);
        for (GoodAttributeValueModel *twoModel in model.twoattribute) {
            if (twoModel.isSelected) {
                isSelect = @(1);
            }
        }
        [selectArr addObject:isSelect];
    }
    
    BOOL select = YES;
    for (NSNumber *number in selectArr) {
        if ([number isEqual:@(0)]) {
            select = NO;
        }
    }
    
    return select;
}

// 判断是否是未选择或者只选择一个属性
+ (BOOL)isSelectNoneOrOneAttribute:(NSMutableArray <GoodAttributeModel *> *)attrArray {
    NSMutableArray *selectArr = [NSMutableArray array];
    for (GoodAttributeModel *model in attrArray) {
        NSNumber *isSelect = @(0);
        for (GoodAttributeValueModel *twoModel in model.twoattribute) {
            if (twoModel.isSelected) {
                isSelect = @(1);
            }
        }
        [selectArr addObject:isSelect];
    }
    
    NSInteger count = 0;
    for (NSNumber *number in selectArr) {
        if ([number isEqual:@(1)]) {
            count += 1;
        }
    }
    return (count == 0 || count == 1);
}

// 获取所有属性
+ (NSMutableArray <NSString *> *)getAllAttributes:(NSMutableArray <GoodAttributeModel *> *)attrArray {
    NSMutableArray *arr = [NSMutableArray array];
    for (GoodAttributeModel *model in attrArray) {
        for (GoodAttributeValueModel *valueModel in model.twoattribute) {
            [arr addObject:valueModel.taid];
        }
    }
    return arr;
}

// 获取所有已选中属性的同级属性。和其他未选择的属性列的 可选属性。
+ (NSMutableArray <NSString *> *)getFilterStr:(NSMutableArray <GoodAttributeValueModel *> *)valueArr
                              withPriceSkuArr:(NSMutableArray <GoodsAttributePriceModel *> *)priceSkuArr
                                      attrArr:(NSMutableArray <GoodAttributeModel *> *)attrArray{
    
    NSArray <GoodAttributeValueModel *> *valueModels = [SKUManager getSelectAttrWithAllAttributeArr:attrArray];
    NSMutableArray <NSString *> *filterArr = [SKUManager getFilterAttrs:valueArr withPriceSkuArr:priceSkuArr andAttributeArr:attrArray];
    for (GoodAttributeValueModel *valueModel in valueModels) {
        NSMutableArray *sameLevel = [SKUManager getSelectAttributeSameLevel:attrArray select:valueModel];
        for (NSString *s in sameLevel) {
            if (![filterArr containsObject:s]) {
                [filterArr addObject:s];
            }
        }
    }
    return filterArr;
}

// 筛选返回可选属性
+ (NSMutableArray <NSString *> *)getCanSelectAttribute:(NSMutableArray <GoodAttributeValueModel *> *)valueArr
                                       withPriceSkuArr:(NSMutableArray <GoodsAttributePriceModel *> *)priceSkuArr
                                               attrArr:(NSMutableArray <GoodAttributeModel *> *)attrArray {
    
    
    NSArray <GoodAttributeValueModel *> *valueModels = [SKUManager getSelectAttrWithAllAttributeArr:attrArray];
    NSMutableArray *cannotSelectArr = [NSMutableArray array];
    
    // 多属性筛选，获取所有的选中同行属性
    NSMutableArray <NSString *> *filterArr = [SKUManager getFilterStr:valueArr withPriceSkuArr:priceSkuArr attrArr:attrArray];
// [SKUManager getFilterAttrs:valueArr withPriceSkuArr:priceSkuArr andAttributeArr:attrArray];
//    for (GoodAttributeValueModel *valueModel in valueModels) {
//        NSMutableArray *sameLevel = [SKUManager getSelectAttributeSameLevel:attrArray select:valueModel];
//        for (NSString *s in sameLevel) {
//            if (![filterArr containsObject:s]) {
//                [filterArr addObject:s];
//            }
//        }
//    }
    NSMutableArray *all = [SKUManager getAllAttributes:attrArray];
    [all removeObjectsInArray:filterArr];
    [cannotSelectArr addObjectsFromArray:all];
    
    
    for (NSInteger i = 0; i < valueArr.count; i++) {
        NSMutableArray *s_arr = [NSMutableArray arrayWithArray:valueArr];
        [s_arr removeObjectAtIndex:i];
        NSMutableArray *s_all = [SKUManager getAllAttributes:attrArray];
        // 获取除了选中属性，其他所有属性的strs
        NSMutableArray *s_str = [SKUManager getFilterAttrs:s_arr withPriceSkuArr:priceSkuArr andAttributeArr:attrArray];
        // 选中的行数，同行添加到可选属性。
        for (GoodAttributeValueModel *value_model in s_arr) {
            [s_str addObjectsFromArray:[SKUManager getSelectAttributeSameLevel:attrArray select:value_model]];
        }
        // 反选所有属性，留下不可选属性
        [s_all removeObjectsInArray:s_str];
        [cannotSelectArr addObjectsFromArray:s_all];
    }

    
    NSMutableArray *allStrs = [SKUManager getAllAttributes:attrArray];
    [allStrs removeObjectsInArray:cannotSelectArr];
    return allStrs;
}


// 获取同级所有属性
+ (NSMutableArray <NSString *> *)getSelectAttributeSameLevel:(NSMutableArray <GoodAttributeModel *> *)attrArray select:(GoodAttributeValueModel *)valueModel {
    NSMutableArray *arr = [NSMutableArray array];
    for (GoodAttributeModel *model in attrArray) {
        if ([model.twoattribute containsObject:valueModel]) {
            for (GoodAttributeValueModel *valueModel in model.twoattribute) {
                [arr addObject:valueModel.taid];
            }
        }
    }
    return arr;
}

// 获取选中select Data
+ (NSArray <GoodAttributeModel *> *)getSelectOriginDataArr:(NSArray <GoodAttributeModel *> *)attrArray
                           withPriceSkuArr:(NSArray <GoodsAttributePriceModel *> *)priceSkuArr
                                  withGaid:(NSString *)gaid {
    GoodsAttributePriceModel *model = [[GoodsAttributePriceModel alloc] init];
    for (GoodsAttributePriceModel *priceModel in priceSkuArr) {
        if ([priceModel.gaid isEqualToString:gaid]) {
            model = priceModel;
        }
    }
    
    for (GoodAttributeModel *attributeModel in attrArray) {
        for (GoodAttributeValueModel *valueModel in attributeModel.twoattribute) {
            for (TwoAtrributeModel *twoModel in model.twoattribute) {
                if ([twoModel.taid isEqualToString:valueModel.taid]) {
                    valueModel.isSelected = YES;
                }
            }
        }
    }
    
    return attrArray;
}

// 过滤一遍，把库存为0的从数组中去掉
+ (NSMutableArray *)getSKUCountPriceItem:(NSArray <GoodsAttributePriceModel *> *)priceSkuArr {
    NSMutableArray *skuArr = [NSMutableArray array];
    for (GoodsAttributePriceModel *priceModel in priceSkuArr) {
        if (priceModel.gastock.integerValue > 0) {
            [skuArr addObject:priceModel];
        }
    }
    return skuArr;
}

@end

/*
#import <Foundation/Foundation.h>
#import "CommodityModel.h"
@interface AttrSelectHelp : NSObject
@property (weak) NSMutableArray *attrArray;//当前所有属性对象
@property (weak) NSArray *skuArray;//当前商品活着类别下的所有sku对象
-(NSMutableArray *) GetSelectsku;//获取已选中的属性值
@end


//说明一下使用到的 CommodityModel，TagModel 我前面已经分享TagModel多添加了一个字段
//@property (nonatomic, assign) BOOL modelLocked;

#import "AttrSelectHelp.h"
#import "CommodityModel.h"
#import "TagModel.h"
@implementation AttrSelectHelp
@synthesize attrArray,skuArray;

//获取已选中的属性值
-(NSMutableArray *) GetSelectAttr{
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    for (CommodityModel *model in attrArray) {
        for (TagModel *tag in model.tagModelArray) {
            if(tag.modelSelected==YES){
                [arr addObject:tag.tagID];
            }
        }
    }
    return arr;
}
//获取已选中父对象CommodityModel
-(NSMutableArray *) GetSelectParents{
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    BOOL isSelect=NO;
    for (CommodityModel *model in attrArray) {
        isSelect=NO;
        for (TagModel *tag in model.tagModelArray) {
            if(tag.modelSelected==YES){
                isSelect=YES;
            }
        }
        if(isSelect==YES){
            [arr addObject:model];
        }
    }
    return arr;
}
//获取未选中父对象CommodityModel
-(NSMutableArray *)GetSelectsku{
    NSMutableArray *filterarr=[self GetfilterAttrs:[self GetSelectAttr]];
    BOOL isSelect=NO;
    for (CommodityModel *model in attrArray) {
        isSelect=NO;
        for (TagModel *tag in model.tagModelArray) {
            if(tag.modelSelected==YES){
                isSelect=YES;
            }
        }
        if(isSelect==NO){
            //设置不可选状态
            [self set_block:model val:filterarr];
        }else{
            [self UpdateAttr:model];
        }
    }
    return attrArray;
}
-(void)UpdateAttr:(CommodityModel *)model{
    NSMutableArray *tarr=[[NSMutableArray alloc]init];
    for (TagModel *tag in model.tagModelArray) {
        if(tag.modelSelected==YES){
            tarr=[self Del_array_val:[self GetSelectAttr] val:[tag.tagID intValue]];
            NSMutableArray *filterarr=[self GetfilterAttrs:tarr];
            [self set_block:model val:filterarr];
            break;
        }
    }
}
// 根据已经选择得属性值，得到余下还能选择的属性值
-(NSMutableArray *)  GetfilterAttrs:(NSMutableArray *)select_ids{
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    NSMutableArray *temp=[self GetfilterProduct:select_ids];//获取可以组合商品sku
    if(temp.count>0){
        for (int i=0; i<temp.count; i++) {
            NSDictionary *dic=temp[i];
            NSString *str=[[NSString alloc]initWithFormat:@"%@",dic[@"saleAttr"]];
            NSArray *a=[str componentsSeparatedByString:@";"];
            if(a.count>0){
                for (int n=0; n<a.count; n++) {
                    [arr addObject:a[n]];
                }
            }
        }
    }
    return arr;
}
//获取所有包含指定节点的路线
-(NSMutableArray *)  GetfilterProduct:(NSMutableArray *)ids{
    NSMutableArray *arr=[[NSMutableArray alloc]init];
    if(skuArray.count>0){
        for (int i=0; i<skuArray.count; i++) {
            NSDictionary *dic=skuArray[i];
            NSString *str=[[NSString alloc]initWithFormat:@"%@",dic[@"saleAttr"]];
            BOOL _all_ids_in=YES;
            for (int i=0;i<ids.count;i++) {
                //NSRange *r=[str rangeOfString:t]
                NSLog(@"%i",[ids[i] intValue]);
                if(![str containsString: [[NSString alloc]initWithFormat:@"%i",[ids[i] intValue]]]){
                    _all_ids_in=NO;//sku不包含此对象
                }
            }if(_all_ids_in==YES){
                [arr addObject:dic];
            }
        }
    }
    return arr;
}

//设置对象是否可选状态
-(void)set_block:(CommodityModel *)tmodel val:(NSMutableArray *)ids{
    NSMutableArray *filterarr=ids;
    for (TagModel *tag in tmodel.tagModelArray) {
        if([filterarr containsObject:[[NSString alloc]initWithFormat:@"%i",[tag.tagID intValue]]]){
            tag.modelLocked=NO;
        }else{
            tag.modelLocked=YES;
        }
    }
}

//去除 数组 arr中的 val ，返回一个新数组
-(NSMutableArray *)  Del_array_val:(NSMutableArray *)arr val:(int)ids{
    NSMutableArray *tarr=[[NSMutableArray alloc]init];
    if(arr.count>0){
        for (int i=0;i<arr.count;i++) {
            if([arr[i] intValue] !=ids){
                [tarr addObject:arr[i]];
            }
        }
        
    }
    return tarr;
}
@end
*/
